package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.CommentMapper;
import cn.tedu.weibo.pojo.dto.CommentDTO;
import cn.tedu.weibo.pojo.entity.Comment;
import cn.tedu.weibo.pojo.vo.CommentDetailVO;
import cn.tedu.weibo.pojo.vo.ComnentListVO;
import cn.tedu.weibo.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class CommentController {
    @Autowired(required = false)
    CommentMapper commentMapper;
    @RequestMapping("/comment/insert")
    public int insert(HttpSession session, @RequestBody CommentDTO commentDTO){
        UserVO userVO=(UserVO) session.getAttribute("user");
        if(userVO==null){
            return 2;
        }
        System.out.println("session = " + session + ", commentDTO = " + commentDTO);
        Comment comment=new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(userVO.getId());
        commentMapper.insert(comment);
        return 1;
    }
    @RequestMapping("/comment/select")
    public List<ComnentListVO> selects(int id){
        return commentMapper.selects(id);
    }
    @RequestMapping("/comment/selectById")
    public CommentDetailVO selectById(int id){
        return commentMapper.selectById(id);
    }
}
